package com.zhihao.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhihao.takeaway.common.utils.DateTool;
import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.common.utils.JwtUtil;
import com.zhihao.takeaway.common.utils.MD5Utils;
import com.zhihao.takeaway.entity.Employee;
import com.zhihao.takeaway.entity.EmployeeAndOrders;
import com.zhihao.takeaway.entity.Orders;
import com.zhihao.takeaway.entity.Person;
import com.zhihao.takeaway.mapper.EmployeeMapper;
import com.zhihao.takeaway.mapper.OrdersMapper;
import com.zhihao.takeaway.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee>
        implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private OrdersMapper ordersMapper;
// 登录校验
    @Override
    public Employee isEmployeeExist(String account) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>();
        queryWrapper.lambda().eq(Employee::getAccount,account);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        return employee;
    }
// 注册
    @Override
    public String enroll(Person person) {
        Employee employee = new Employee();
        employee.setUsername(person.getUsername());
        employee.setPhone(person.getPhone());
        employee.setIdNumber(person.getIdNumber());
        employee.setPassword(person.getPassword());
        employee.setSex(person.getSex());
        employee.setName(person.getName());
        employee.setUpdateTime(DateTool.getCurrTime());
        employee.setCreateTime(DateTool.getCurrTime());
        employee.setUpdateUser("1");
        employee.setStatus(0);
        employee.setCreateUser("1");
//        进行md5加密
        String password = employee.getPassword();
        employee.setPassword(MD5Utils.inputPassToFormPass(password));
//        查询注册总数
        Integer num = employeeMapper.selectCount(null);
        String account =String.valueOf(num+10000);
        account = "YG"+account;
        employee.setAccount(account);
        employeeMapper.insert(employee);
        return account;
    }
// 判断是否信息已经存在
    @Override
    public Boolean isEnroll(Person person) {
        Employee employee = new Employee();
        employee.setUsername(person.getUsername());
        employee.setIdNumber(person.getIdNumber());
        employee.setPhone(person.getPhone());
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Employee::getUsername,employee.getUsername())
                .or().eq(Employee::getIdNumber,employee.getIdNumber())
                .or().eq(Employee::getPhone,employee.getPhone());
        List<Employee> list =  employeeMapper.selectList(queryWrapper);
        if(list.size() == 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Integer updateEmployee(Employee employee) {
        employee.setUpdateTime(DateTool.getCurrTime());
        Integer num = employeeMapper.updateById(employee);
        return num;
    }

    @Override
    public Integer deleteById(String id) {
        Integer num = employeeMapper.deleteById(id);
        return num;

    }

    @Override
    public EasyUITable getAllData(Integer currentPage) {
        IPage page = new Page(currentPage,5);
        employeeMapper.selectPage(page,null);
        List<Employee> employees = page.getRecords();
        for(int i = 0 ; i < employees.size() ; i++){
            if(employees.get(i).getAccount().equals("admin")){
                employees.remove(i);
            }
        }
        Integer total = (int)page.getTotal();
        return new EasyUITable(total,employees);
    }

    @Override
    public String selectUserName(){
        String token = request.getHeader("token");
        String EmployeeId = JwtUtil.validateToken(token);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Employee::getId,EmployeeId);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        if(employee != null){
            return employee.getUsername();
        }else{
            return null;
        }
    }
// 出现父转子强转失败的情况，出此下策
    @Override
    public EmployeeAndOrders getEmployeeInfo(String employeeId) {
        Employee employee =  employeeMapper.selectById(employeeId);
        EmployeeAndOrders employeeAndOrders = new EmployeeAndOrders();
        employeeAndOrders.setIdNumber(employee.getIdNumber());
        employeeAndOrders.setPhone(employee.getPhone());
        employeeAndOrders.setUpdateTime(employee.getUpdateTime());
        employeeAndOrders.setUsername(employee.getUsername());
        employeeAndOrders.setAccount(employee.getAccount());
        employeeAndOrders.setSex(employee.getSex());
        employeeAndOrders.setCreateTime(employee.getCreateTime());
        QueryWrapper<Orders>queryWrapper = new QueryWrapper<>();
        QueryWrapper<Orders>queryWrapper2 = new QueryWrapper<>();
        QueryWrapper<Orders>queryWrapper3 = new QueryWrapper<>();
        queryWrapper.lambda().eq(Orders::getEmployeeId,employeeId);
        queryWrapper2.lambda().eq(Orders::getEmployeeId,employeeId);
        queryWrapper3.lambda().eq(Orders::getEmployeeId,employeeId);
        queryWrapper.lambda().eq(Orders::getStatus,4);
        queryWrapper2.lambda().eq(Orders::getStatus,4);
        queryWrapper3.lambda().eq(Orders::getStatus,4);
        String day = DateTool.getCurrDay();
        String month = DateTool.getCurrMon();
        String year = DateTool.getCurrYear();
        queryWrapper.lambda().like(Orders::getCompleteTime,day);
        Integer dayTradeVolume = ordersMapper.selectCount(queryWrapper);
        queryWrapper2.lambda().like(Orders::getCompleteTime,month);
        Integer monTradeVolume = ordersMapper.selectCount(queryWrapper2);
        queryWrapper3.lambda().like(Orders::getCompleteTime,year);
        Integer yearTradeVolume = ordersMapper.selectCount(queryWrapper3);
        employeeAndOrders.setDayTradeVolume(dayTradeVolume);
        employeeAndOrders.setMonthTradeVolume(monTradeVolume);
        employeeAndOrders.setYearTradeVolume(yearTradeVolume);
        return employeeAndOrders;
    }

    public String getEmployeeId(){
        String token = request.getHeader("token");
        String EmployeeId = JwtUtil.validateToken(token);
        return EmployeeId;
    }
}
